layui.use(['form', 'laydate'], function() {
	var form = layui.form,
		laydate = layui.laydate,
		$ = layui.jquery,
		upload = layui.upload,
		table = layui.table;

	laydate.render({
		elem: '#birthday'
	});

	/*动态加载班级下拉框*/
	form.on('select(selgradename)', function(data) {
		var schoolid = $("#selschool").val();
		var gradname = data.value;
		if (schoolid == 00 || schoolid == 'null' || schoolid == null) {
			layer.msg("请选择学校");
			return;
		}
		if (gradname == 00 || gradname == 'null' || gradname == null) {
			gradname = "";
		}
		$("#classid").html("");
		$.ajax({
			type: 'get',
			url: '../sysclassmanagement/getclassbyschoolandgrade?schoolid=' + schoolid + '&gradename=' + gradname,
			datatype: 'json',
			success: function(classdata) {
				if (classdata.code == 0) {
					var str = "<option value='00'>请选择班级</option>";
					for (var i = 0; i < classdata.count; i++) {
						str += '<option value=' + classdata.data[i].classid + '>' + classdata.data[i].className + '</option>';
					}
					$("#classid").append(str);
					form.render();
				} else {
					layer.msg('班级信息获取失败', {
						icon: 2
					});
					$("#classid").html("");
					form.render("select");
				}
			}
		});
	});

	//加载阶段下拉框
	loadstage("stage", form);

	//根据阶段加载学校，年级下拉框
	form.on('select(stage)', function(data) {
		//年级下拉框
		loadgrade(data.value, "grade", form);
		//学校下拉框
		loadSchool(data.value, "school", form);

	});

	//根据年级和学校加载班级下拉框
	form.on('select(grade)', function(data) {

		var schoolid = $("#school").val();
		if (schoolid == null || schoolid == "" || schoolid == "0") {
			layer.alert('请先选择学校！', {
				icon: 2
			});
			return;
		}
		//班级下拉框加载
		loadClass(schoolid, data.value, "classname", form);

	});
	
	form.on('select(classid)', function(data) {
		var classid=data.value;
		$.ajax({
			type : 'get',
			url : '../studentmanagement/getsyuusername?classid='+classid,
			datatype : 'json',
			success : function(classdate) {
				if (classdate.code == 10001) {
					$("#stuid").val(classdate.stuid);
				}
				else{
					layer.msg(classdate.msg,{icon:2});
				}
			}
		});
	});

	$("#downloadteacher").click(function() {

		$.ajax({
			type: 'get',
			url: '../studentmanagement/downloadstudent',
			datatype: 'json',
			success: function(data) {
				if (data.code == 10001) {
					var link = document.createElement("a");
					link.href = data.path;
					link.style = "visibility:hidden";
					link.download = "学生信息表模板";
					document.body.appendChild(link);
					link.click();
					document.body.removeChild(link);
				} else {
					layer.alert(data.msg, {
						icon: 2
					});
				}
			},
			error: function() {
				layer.alert('下载失败！', {
					icon: 2
				});
			}
		});

	});

	$("#saveStuList").click(function() {
		$.ajax({
			type: 'get',
			url: '../studentmanagement/savestuList',
			datatype: 'json',
			success: function(data) {
				if (data.code == 0) {
					layer.alert(data.msg, {
						icon: 1
					});
				} else {
					layer.alert(data.msg, {
						icon: 2
					});
				}
			},
			error: function() {}
		});

	});

	$("#btnselExcel").click(function() {
		var classname = $("#classname").val();
		var school = $("#school").val();
		if (classname != "" && classname != "0" && school != "" && school != "0") {
			$("#selExcel").click();
			$("#saveStuList").attr("disabled", false);
			$("#saveStuList").removeClass("layui-disabled");
		} else {
			$("#saveStuList").attr("disabled", true);
			$("#saveStuList").addClass("layui-disabled");
			layer.alert('请先选择班级！', {
				icon: 2
			});
		}
	});

	$("#selExcel").click(function() {
		var classid = $("#classname").val();
		var school = $("#school").val();
		//指定允许上传的文件类型
		upload.render({ //允许上传的文件后缀
			elem: '#selExcel',
			url: '../studentmanagement/getexceltablelist?classid=' + classid + '&schoolid=' + school,
			accept: 'file', //普通文件
			exts: 'xls|xlsx|xlsm', //只允许上传压缩文件
			acceptMime: 'xls|xlsx|xlsm',
			done: function(res) {
				if (res.code == 0) {
					table.render({
						elem: '#teacher',
						url: '../studentmanagement/excelloadtable',
						title: '学生数据表',
						skin: 'line',
						even: true,
						cols: [
							[{
								type: 'numbers',
								title: '编号'
							}, {
								field: 'stuid',
								title: '账号'
							}, {
								field: 'realName',
								title: '真实姓名'
							}, {
								field: 'sex',
								title: '性别'
							}, {
								field: 'birthday',
								title: '生日'
							}]
						]
					});
				} else {
					layer.alert(data.msg, {
						icon: 2
					});
				}
			}
		});
	});

	/* 添加一个学生信息 */
	form.on('submit(btnaddstu)', function(studata) {
		var index = layer.load(0, {
			shade: 0.1
		});
		var stuid = $("#stuid").val();
		var pwd = "000000";
		var realName = $("#realName").val();
		var sex = $("input[name='sex']:checked").val();
		var classid = $("#classid").val();
		var schoolid = $("#selschool").val();
		var birthday = $("#birthday").val();
		if (schoolid == 00 || schoolid == null || schoolid == 'null') {
			layer.close(index);
			layer.msg('请选择学校！', {icon: 1});
		}
		if (classid == 00 && classid == null || classid == 'null') {
			layer.close(index);
			layer.msg('请选择班级！', {icon: 1});
		}
		$.ajax({
			url: "../studentmanagement/addstudent",
			type: "get",
			dataType: "json",
			data: {
				schoolid: schoolid,
				stuid: stuid,
				pwd: pwd,
				realName: realName,
				sex: sex,
				classid: classid,
				birthday: birthday
			},
			success: function(getdata) {
				if (getdata.code == 10001) {
					layer.close(index);
					layer.confirm('学生信息添加成功！', {
						btn: ['确定']
					}, function() {
						window.location.href = "../studentManagement/editStudent.jsp";
						$("#liformtitle").text("学生信息查看");
					});
				} else {
					layer.close(index);
					layer.alert('学生添加失败', {
						icon: 2
					});
					return false;
				}
			},
			error : function() {
				layer.close(index);
				layer.alert('出现错误，请重试！', {icon: 2});
			}
		});
	});

});